// PSerialPort_Sigma.cpp: implementation of the CPSerialPort_Sigma class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SerialPort_Sigma.h"

#include "gtcom.h"
#pragma comment(lib,"gtcom.lib")


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSerialPort_Sigma::CSerialPort_Sigma()
{
	m_hComm=INVALID_HANDLE_VALUE;
	usComName = 0;
	usComMode = 2; //??0-RS232,1-RS485,2-RS422
	ulBaudrate = 19200;// baudrate????,??9600
	ucStopBits = 1;// Stopbits????,??0??????,??2??????
	ucParity = 0;// Parity????,??0????,??1?ODD??,??2?EVEN??
	ucWordLength = 8;// WordLength?????,?????8,8???
}

CSerialPort_Sigma::~CSerialPort_Sigma()
{
}

short CSerialPort_Sigma::OpenPort(LPCTSTR Port,int BaudRate,int DataBits,int StopBits,int Parity)
{
	short sRtn = 0;
	UCHAR aucState[6];

	ulBaudrate = BaudRate;
	ucStopBits = StopBits;
	ucParity = Parity;
	//??FPGA?????,index?????,?0????,???????4???
	sRtn += GTCOM_Open(usComName);
	//?????485,0-RS232,1-RS485,2-RS422
	sRtn += GTCOM_SetMode(usComName, usComMode);
	//??????
	//index???????????
	//???:bit[3:0]????; bit[5:4]?????????
	//				Bit[3:0] :  1  ---   err        0   ---  ok 
	//				Bit0:???????			Bit1: ??????			Bit2:??buffer?????			Bit3: ???????
	//				Bit4: ?????????, 1 ?? ???????????????		0 ???????,?????
	//				Bit5:?????????,1 ???????????,????			0 ?????????,?????
	sRtn += GTCOM_GetComState(usComName, aucState);
	//??????
	// index?????????
	// baudrate????,??9600,??100?300?600?1200?2400?4800?9600?19200?57600?115200
	// Stopbits????,??0??????,??2??????
	// Parity????,??0????,??1?ODD??,??2?EVEN??
	// WordLength?????,?????8,8???
	sRtn += GTCOM_SetComState(usComName, ulBaudrate,ucStopBits,ucParity,ucWordLength);
	
	m_hComm = (HANDLE)1;

	return RTN_OK;
}


BOOL CSerialPort_Sigma::ClosePort()
{
	if(m_hComm!=INVALID_HANDLE_VALUE)
	{
		GTCOM_Close(usComName);
		m_hComm=INVALID_HANDLE_VALUE;
		return TRUE;
	}
	else
		return TRUE;	
}



//???????
DWORD CSerialPort_Sigma::WritePortEx(unsigned char *data,int length)
{
	if(m_hComm==INVALID_HANDLE_VALUE)
	{
		return 0;
	}

	DWORD dwBytesWritten=0;

	dwBytesWritten=GTCOM_Write(usComName, (unsigned char*)data, length);	

	return dwBytesWritten;
}




DWORD CSerialPort_Sigma::WritePort(char *data,int length)
{
	if(m_hComm==INVALID_HANDLE_VALUE)
	{
		return 0;
	}

	DWORD dwBytesWritten=0;

	dwBytesWritten=GTCOM_Write(usComName, (unsigned char*)data, length);	
	
	return dwBytesWritten;
}






DWORD CSerialPort_Sigma::ReadPortEx(unsigned char *data,int length)
{
	int sRtn = 0;
	DWORD dwBytesRead;

	if(m_hComm != INVALID_HANDLE_VALUE)
	{
		
		Sleep(80); //200
		//??????10??????
		dwBytesRead = GTCOM_Read(usComName, length, (unsigned char*)data);	
		return dwBytesRead;
	}
	else
	{
		return 0;
	}
	
}



DWORD CSerialPort_Sigma::ReadPort(char *data,int length)
{
	int sRtn = 0;
	DWORD dwBytesRead;

	if(m_hComm != INVALID_HANDLE_VALUE)
	{

		Sleep(200);
		//??????9??????
		dwBytesRead = GTCOM_Read(usComName, length, (unsigned char*)data);	
		return dwBytesRead;
	}
	else
	{
		return 0;
	}
}



